package com.zjitc.service.impl;

import com.zjitc.common.CommonUtils;
import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UsersDao;
import com.zjitc.po.User;
import com.zjitc.service.UserService;
import com.zjitc.token.Cache;
import com.zjitc.token.Redis;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 14:56
 * @description:
 */

//实现类
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UsersDao usersDao;

  @Autowired
  private Cache cache;

  @Autowired
  private Jedis jedis;

  // 2.4.1 登录
  @Override
  public ServerResponse<User> login(String username, String password) {
    int count = usersDao.checkUsername(username);
    if (count != 1) {
      return ServerResponse.createSucess("用户名不存在");
    }
    User user = usersDao.findByUsernameAndPassword(username, password);
    if (user == null) {
      return ServerResponse.createError("校验成功");
    }
    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    return ServerResponse.createSucess(user);
  }

  // 2.4.2 注册
  @Override
  public ServerResponse<User> register(User user) {
    int count = usersDao.checkUsername(user.getUsername());
    if (count == 1) {
      return ServerResponse.createError("用户已存在");
    }
    user.setRole(0);
    user.setUpdate_time(new Date());
    user.setCreate_time(new Date());
    int abc = usersDao.register(user);
    return ServerResponse.createSucess("校验成功");
  }

  // 2.4.3 检查用户名是否有效
  @Override
  public ServerResponse<User> check_valid(String str, String type) {
    if (jedis.exists(type)){
      if (jedis.sismember(type,str)){
        return ServerResponse.createError("用户名已存在");
      }
    }
    return ServerResponse.createSucess("校验成功");
  }
  // 2.4.4 获取登录用户信息
  @Override
  public ServerResponse<User> get_user_info(Integer id) {
    User user = usersDao.get_User_Info(id);
    user.setAnswer(null);
    user.setPassword(null);
    user.setQuestion(null);
    return ServerResponse.createSucess(user);
  }

  // 2.4.5 忘记密码
  @Override
  public ServerResponse<User> forget_get_question(String username) {
    String question = usersDao.forget_get_question(username);
    if (question.equals("问题")) {
      return ServerResponse.createError("该用户未设置找回密码问题");
    } else {
      return ServerResponse.createError(question);
    }
  }

  //2.4.6 提交问题答案
  @Override
  public ServerResponse<User> forget_check_answer(String username, String question, String answer) {
    String abc = usersDao.reset_password3(username);
    if (!abc.equals(answer)) {
      return ServerResponse.createError("问题答案错误");
    }
    String token = CommonUtils.uuid();
    cache.add(username, token);
    return ServerResponse.createSucess(token);
  }
  //2.4.7 忘记密码的重设密码
  @Override
  public ServerResponse<User> forget_reset_password(String username, String passwordNew,
      String forgetToken) {
    return null;
  }

  // 2.4.8 登录中状态重置密码
  @Override
  public ServerResponse<User> reset_password(Integer id, String passwordOld, String passwordNew) {
    String username = usersDao.reset_password1(id);
    if (!username.equals(passwordOld)) {
      return ServerResponse.createError("旧密码输入错误");
    }
    User user = new User();
    user.setId(id);
    user.setPassword(passwordNew);
    int count = usersDao.reset_password2(user);
    return ServerResponse.createSucess("修改密码成功");
  }

  //2.4.9 登录状态更新个人信息
  @Override
  public ServerResponse<User> update_information(User user, Integer id) {
    user.setId(id);
    int count = usersDao.reset_password2(user);
    return ServerResponse.createSucess("更新个人信息成功");
  }

  //2.4.10 获取当前登录用户的详细信息，并强制登录
  @Override
  public ServerResponse<User> get_information(Integer id) {
    User user = usersDao.get_information(id);
    return ServerResponse.createSucess(user);
  }
}
